package com.apimarket.service;

import com.apimarket.dao.PublishMapper;
import com.apimarket.entity.Publish;
import com.apimarket.enums.ExceptionEnum;
import com.apimarket.exception.AMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.List;

/**
 * xuyang
 */
@Transactional
@Service
public class PublishService {

    @Autowired(required = false)
    private PublishMapper mapper;

    public List<Publish> queryList(Publish entity) {
        List<Publish> list = mapper.queryList(entity);
        if(CollectionUtils.isEmpty(list)){
            throw new AMException(ExceptionEnum.Publish_NOT_FOUND);
        }
        return list;
    }

    public void insert(Publish entity) {
        int sign;
        try {
            sign=mapper.insert(entity);
        }catch (Exception e
        ){
            throw new AMException(ExceptionEnum.Publish_INSERT_ERROR);
        }
        if(sign!=1){
            throw new AMException(ExceptionEnum.Publish_INSERT_ERROR);
        }
    }

    public void update(Publish entity) {
        int sign;
        try {
            sign=mapper.update(entity);
        }catch (Exception e){
            throw new AMException(ExceptionEnum.Publish_UPDATE_ERROR);
        }
        if(sign!=1){
            throw new AMException(ExceptionEnum.Publish_UPDATE_ERROR);
        }
    }

    public void delete(Integer id) {
        int sign;
        try {
            sign=mapper.delete(id);
        }catch (Exception e){
            throw new AMException(ExceptionEnum.Publish_DELETE_ERROR);
        }
        if(sign!=1){
            throw new AMException(ExceptionEnum.Publish_DELETE_ERROR);
        }
    }
}
